﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

namespace GlobalLib.UI
{
    public class JSONHandler : HttpUntity, IHttpHandler
    {
        public bool IsReusable { get { return false; } }

        #region props
        protected HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;

            }
        }

        protected HttpServerUtility Server
        {
            get
            {
                return HttpContext.Current.Server;
            }
        }
        #endregion

        public void ProcessRequest(HttpContext context)
        {
            Response.Clear();
            try
            {
                OnLoad();
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                // logging;
                throw ex;
            }
        }

        protected virtual void OnLoad()
        {

        }

        /// <summary>
        /// 一旦输出将会立即停止Handler
        /// </summary>
        /// <param name="result"></param>
        protected void PrintfJSON(object result) {
            string jsonString = JsonHelper.JsonSerializer<object>(result);
            Response.Write(jsonString);
            Response.End();
        }

        public class JSONResult {
            public int code;
            public string message;

            public string Message
            {
                get { return message; }
                set { message = value; }
            }

            public int Code {
                get { return code; }
                set { code = value; }
            }
        }
    }
}
